import TokenManager from '@/framework/manager/token-manager'
import { defineStore } from 'pinia'

import { useRouter } from 'vue-router';

const router = useRouter();

export const useUserStore = defineStore('user', {

    state:() => ({
        userId: null,
        isLoggedIn: false,
        permissions: [''],
        user: null

    }),

    actions:{
      // 登录成功
      loginSuccess(data:any){
          this.isLoggedIn = true;
          this.userId = data.user_id;
          this.loadUserInfo();
      },
      /**
       * 加载用户信息
       */
      loadUserInfo(){
          // TODO
      },

      /**
       * 登录
       */
      login(){
        console.log('logout')
        this.isLoggedIn = false;
        this.userId = null;
        this.permissions = [];
        this.user = null;
        TokenManager.clear()
      },

        // 登出
      logout(){
        console.log('logout')
        this.isLoggedIn = false;
        this.userId = null;
        this.permissions = [];
        this.user = null;
        TokenManager.clear()
        router.replace('/')
      }


  }

})

